package pers.mingshan.thread.thread;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 线程范围内数据共享
 * @author mingshan
 *
 */
public class ThreadScopeShareDate {

    private static Map<Thread,Integer> map=new HashMap<Thread,Integer>();

    public static void main(String[] args) {
        for (int i = 0; i < 2; i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int data=new Random().nextInt();
                    System.out.println("ThreadName - " + Thread.currentThread().getName() + data);
                    map.put(Thread.currentThread(), data);
                    new A().get();
                    new B().get();
                }
            }).start();
        }
    }

    static class A {
        public void get() {
            System.out.println("A  module - " + Thread.currentThread().getName() + map.get(Thread.currentThread()));
        }
    }

    static class B {
        public void get() {
            System.out.println("B  module - " + Thread.currentThread().getName() + map.get(Thread.currentThread()));
        }
    }
}
